<?php
namespace App\WebSocket\Controller;

use App\Utils\Common;
use App\Utils\LogHelp;
use App\Utils\WebSocketMessage;
use EasySwoole\Socket\AbstractInterface\Controller;
use EasySwoole\Socket\Client\WebSocket as WebSocketClient;
use Exception;
use Google\Protobuf\Any;
use Jubo\JuLiao\IM\Wx\Proto\TransportMessage;

/**
 * 基础控制器
 * Class Base
 * @package App\WebSocket\Controller
 */
class Base extends Controller
{
    /**
     * 请求用户的进程ID
     * @var int $fd
     */
    public $fd;
    /**
     * @var array|null 原始数据
     */
    public $data;
    /**
     * @var array|string 内容数据
     */
    public $content;

    public $noCheckAction = [];


    /**
     * 解析数据
     */
    protected function parseData()
    {
        $this->fd = $this->caller()->getClient()->getFd();

        $data = $this->caller()->getArgs(); // 获取请求参数
        $this->data = $data['data'];
        $this->content = $data['data']['content'];
    }

    /**
     * 解析数据【请求控制器前执行的方法】
     * @param string|null $actionName
     * @return bool
     */
    protected function onRequest(?string $actionName): bool
    {
        // 解析数据
        $this->parseData();
        // 判断来源
        if (!in_array($this->data['checkType'], [WebSocketMessage::CheckTypeBy, WebSocketMessage::CheckTypeScreen])) {
            return false;
        }
        // 验证token
        if (!in_array($actionName, $this->noCheckAction)) {
            if ($this->data['checkType'] == WebSocketMessage::CheckTypeScreen) {// 大屏
                if (!isset($this->data['token'])) {
                    $data = WebSocketMessage::getErrorMessage("token不能为空");
                    $this->response()->setMessage($data);
                    return false;
                }

                $data = Common::decodeToken($this->data['token']);
                if ($data === false) {
                    $data = WebSocketMessage::getErrorMessage("无效token");
                    $this->response()->setMessage($data);
                    return false;
                }
                $projectId = intval($data['project_id'] ?? 0);
                $account = $data['account'] ?? '';
                $password = $data['password'] ?? '';

                $projectAccount = Common::getProjectAccount($projectId);
                if (empty($projectAccount)) {
                    $data = WebSocketMessage::getErrorMessage("项目未配置，无法登陆");
                    $this->response()->setMessage($data);
                    return false;
                }
                if ($projectAccount['account'] != $account || $projectAccount['password'] != $password) {
                    $data = WebSocketMessage::getErrorMessage("账号or密码错误");
                    $this->response()->setMessage($data);
                    return false;
                }
            }
        }
        return parent::onRequest($actionName);
    }

    /**
     * 获取当前的用户
     * @return array|string
     * @throws Exception
     */
    protected function currentUser()
    {
        /** @var WebSocketClient $client */
    }

    protected function actionNotFound(?string $actionName)
    {
        // 关闭客户端
//        $fd = $this->caller()->getClient()->getFd();// 请求用户的fd
//        \EasySwoole\EasySwoole\ServerManager::getInstance()->getSwooleServer()->close($fd);
        $msg = "您的请求 {$actionName} 不存在 ... \n";
        $this->response()->setMessage($msg); // 推送消息
    }

    protected function afterAction(?string $actionName)
    {
        $fd = $this->caller()->getClient()->getFd();// 请求用户的fd
        LogHelp::info("进程：{$fd} 指令：{$actionName}执行之后", 'websocket');
    }
}
